package org.jeecg.modules.order.service.impl;

import org.jeecg.modules.order.entity.RsOrder;
import org.jeecg.modules.order.entity.RsOrderDetail;
import org.jeecg.modules.order.mapper.RsOrderDetailMapper;
import org.jeecg.modules.order.mapper.RsOrderMapper;
import org.jeecg.modules.order.service.IRsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 订单表
 * @Author: jeecg-boot
 * @Date:   2020-08-07
 * @Version: V1.0
 */
@Service
public class RsOrderServiceImpl extends ServiceImpl<RsOrderMapper, RsOrder> implements IRsOrderService {

	@Resource
	private RsOrderMapper rsOrderMapper;
	@Resource
	private RsOrderDetailMapper rsOrderDetailMapper;

	@Override
	@Transactional
	public void saveMain(RsOrder rsOrder, List<RsOrderDetail> rsOrderDetailList) {
		rsOrderMapper.insert(rsOrder);
		if(rsOrderDetailList!=null && rsOrderDetailList.size()>0) {
			for(RsOrderDetail entity:rsOrderDetailList) {
				//外键设置
				entity.setOrderId(rsOrder.getId());
				rsOrderDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(RsOrder rsOrder,List<RsOrderDetail> rsOrderDetailList) {
		rsOrderMapper.updateById(rsOrder);

		//1.先删除子表数据
		rsOrderDetailMapper.deleteByMainId(rsOrder.getId());

		//2.子表数据重新插入
		if(rsOrderDetailList!=null && rsOrderDetailList.size()>0) {
			for(RsOrderDetail entity:rsOrderDetailList) {
				//外键设置
				entity.setOrderId(rsOrder.getId());
				rsOrderDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		rsOrderDetailMapper.deleteByMainId(id);
		rsOrderMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			rsOrderDetailMapper.deleteByMainId(id.toString());
			rsOrderMapper.deleteById(id);
		}
	}

}
